<?php
/**
 * Display Ressource Activity Summary
 */

use Doctrine\DBAL\Connection;
use Hive\Mission;
use Hive\Ressource;
use Fox\Dao\SqlFragment;

$xview = Xview::instance();
$grid = new Fox\Grid("gActivites");
$grid->fieldcat(array("label","number"));

$nbAppointment = $nbNew = $nbWip = 0;
// Load nb by status
$query = \Fox\Dao::infos(Ressource::className())->handle()->createQueryBuilder();
$query->select('status','COUNT(*) as nb')
	  ->from('ressources','v')
	  ->groupBy(array("status"));
$fragment = SqlFragment::emptyFragment();
$result = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $status)
{
	switch($status['status'])
	{
		case Ressource::STATUS_WIP;
			$nbWip = $status['nb'];
			break;
		case Ressource::STATUS_NEW;
			$nbNew = $status['nb'];
			break;
	}
}
// Number of sheet to validate
$data[] = array("label" => Msg(Hive::BUNDLE,Hive::NUMBER_OF_CONTACT_TO_VALIDATE),
				"number" => $nbWip);
// Number of new leads
$data[] = array("label" => Msg(Hive::BUNDLE,Hive::NUMBER_OF_NEW_CONTACTS),
				"number" => $nbNew);


// Number of mission
$query = \Fox\Dao::infos(Ressource::className())->handle()->createQueryBuilder();
$query->select('COUNT(*) as nb')
	  ->from('missions','m');

$fragment = new SqlFragment("status NOT IN (?) OR status IS NULL ",
						array(array(Mission::STATUS_CLOSED)),
						array(Connection::PARAM_STR_ARRAY));

$nbMissions = $fragment->pushIn($query)->execute()->fetchColumn();
$data[] = array("label" => Msg(Hive::BUNDLE,Hive::NUMBER_OF_JOB_TO_WORK_ON),
				"number" => $nbMissions);
$grid->initialize($data);

if($xview->isPrintMode() || $xview->ajax()) {
	return $grid->display();
}
else
{
	$screen = new Fox\Html\Screen("sActivities",null);
	$screen->pageTitle(Msg(Hive::BUNDLE,Hive::REPORT_ACTIVITY_SUMMARY));
	$screen->add($grid);
	echo $screen->display();
}